/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.Server;

import Exceptions.IncompatibleVectorSizesException;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import vectors.*;

/**
 *
 * @author Кирилл
 */
public class Client {

    private static Socket socket;

    public Client(int port) throws UnknownHostException, IOException {
        socket = new Socket("localhost", port);
    }

    public static VectorIn sum(VectorIn vc1, VectorIn vc2) throws UnknownHostException, IOException {
        Client cl = new Client(7777);
        VectorIn resultVector = null;
        try {
            InputStream in = socket.getInputStream();
            OutputStream out = socket.getOutputStream();
            Vectors.outputVector(vc1, out);
            System.out.println(vc1.toString());
            Vectors.outputVector(vc2, out);
            System.out.println(vc2.toString());
            resultVector = Vectors.inputVector(in);
            System.out.println(resultVector.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultVector;
    }

    public static void main(String[] args) throws UnknownHostException, FileNotFoundException, IOException, IncompatibleVectorSizesException {
        String path = "D:\\Drive(E)\\ONMU\\progr\\Java\\NetBeans\\nc-training-center-repo\\students\\versanov\\Vectors\\src\\";
        String input = "in.bin";
        String output = "out.txt";
        VectorIn vector;
        VectorIn vector1;
        VectorIn resultVector;
        OutputStream out;
        InputStream in;
        Socket socket = null;
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader(path + input);
            fw = new FileWriter(path + output);
            while (fr.ready()) {
                vector = null;
                vector1 = null;

                vector = Vectors.readVector(fr);
                vector1 = Vectors.readVector(fr);

                resultVector = sum(vector, vector1);
                Vectors.writeVector(resultVector, fw);
                fw.flush();

            }
        } catch (UnknownHostException e) {
            System.out.println("Сокет:" + e.getMessage());
        } catch (EOFException e) {
            System.out.println("EOF:" + e.getMessage());
        } catch (IOException e) {
            System.out.println("readline:" + e.getMessage());
        } finally {
            try {
                fw.close();
                fr.close();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
